package com.lei.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lei.order.entity.Order;
import com.lei.order.service.OrderService;
import com.lei.utils.JwtUtils;
import com.lei.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-16
 */
@Api("订单接口")
@RestController
@RequestMapping("/edu/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //生成订单的方法
    @PostMapping("/createOrder/{courseId}")
    public Result saveOrder(@PathVariable("courseId") String courseId, HttpServletRequest request){
       String orderNo = orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        System.out.println(orderNo);
       return Result.ok().data("orderId",orderNo);
    }

    // 根据订单号查询订单信息
    @GetMapping("/getOrderInfo/{orderNo}")
    public Result getOrderInfo(@PathVariable("orderNo") String orderNo){
        Order order = orderService.getOne(new QueryWrapper<Order>().eq("order_no", orderNo));
        return Result.ok().data("item",order);
    }

    // 根据课程ID和用户ID去判断该客户是否买过该课程
    @GetMapping("/isBuy/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable("courseId") String courseId,@PathVariable("memberId") String memberId){
        return orderService.isBuyCourse(courseId,memberId);
    }
}

